﻿using System;
using System.Xml;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Windows.Forms;
using System.Reflection;
using Mario.GameEngine.GameSructures;
using Mario.GameEngine;
using Mario.Properties;

namespace Mario.DataFiles
{
    public class XMLAccess
    {
        private const int brikWidth = 50;
        private const int brikHeight = 50;
        private const int groundY = 668;

        /// <summary>
        /// Save score results of player
        /// </summary>
        /// <param name="Name">Player name</param>
        /// <param name="Score">Player score</param>
        public void SaveToXML(string Name, int Score)
        {

            XmlDocument doc = new XmlDocument();
            Dictionary<int, string> scoreDictionary = new Dictionary<int, string>();
            scoreDictionary.Add(Score, Name);
            if (File.Exists("Score.xml"))
            {
                doc.Load("Score.xml");
                XmlNodeList nodeList = doc.SelectNodes("/Highscores/HightScore");
                foreach (XmlNode node in nodeList)
                {
                    try
                    {
                        scoreDictionary.Add(Convert.ToInt32(node["Score"].InnerText), node["Name"].InnerText);
                    }
                    catch
                    {

                    }
                }
            }
            doc = new XmlDocument();
            var list = scoreDictionary.Keys.ToList();
            list.Sort();
            XmlDeclaration xmlDeclaration = doc.CreateXmlDeclaration("1.0", "UTF-8", null);
            XmlElement root = doc.DocumentElement;
            doc.InsertBefore(xmlDeclaration, root);

            XmlElement element1 = doc.CreateElement("Highscores");
            doc.AppendChild(element1);
            int minus = 0;
            if (list.Capacity > 10)
            {
                minus = 0;
            }
            else
            {
                minus = -1;
            }

            for (int i = list.Capacity - 1; i > minus; i--)
            {
                XmlElement element2 = doc.CreateElement("HightScore");
                element1.AppendChild(element2);


                XmlElement element3 = doc.CreateElement( "Name");
                XmlText text1 = doc.CreateTextNode(scoreDictionary[list[i]]);
                element3.AppendChild(text1);
                element2.AppendChild(element3);

                XmlElement element4 = doc.CreateElement( "Score");
                XmlText text2 = doc.CreateTextNode(Convert.ToString(list[i]));
                element4.AppendChild(text2);
                element2.AppendChild(element4);
            }                     
            
            doc.Save("Score.xml");
        }

        /// <summary>
        /// Returns score table
        /// </summary>
        /// <returns></returns>
        public Dictionary<int, string> GetDataFromXML()
        {
            XmlDocument doc = new XmlDocument();
            Dictionary<int, string> scoreDictionary = new Dictionary<int, string>();
            if (File.Exists("Score.xml"))
            {
                doc.Load("Score.xml");
                XmlNodeList nodeList = doc.SelectNodes("/Highscores/HightScore");
                foreach (XmlNode node in nodeList)
                {
                    try
                    {
                        scoreDictionary.Add(Convert.ToInt32(node["Score"].InnerText), node["Name"].InnerText);
                    }
                    catch
                    {

                    }
                }
            }
            return scoreDictionary;
        }

        /// <summary>
        /// Get game objects from xml file
        /// </summary>
        /// <param name="fileName">XML file name</param>
        /// <returns></returns>
        public GameObjects GetOBjectsFromXML(string fileName)
        {
            GameObjects gameObject = new GameObjects();

            XmlDocument doc = new XmlDocument();
            Dictionary<int, string> scoreDictionary = new Dictionary<int, string>();


            doc.LoadXml(Resources.Resource1.World1);
                XmlNodeList nodeList = doc.SelectNodes("/Structures/Bricks/Brick");
                List<Bricks> bricks = new List<Bricks>();
                foreach (XmlNode node in nodeList)
                {
                    try
                    {
                        Bricks brick = new Bricks();
                        brick.Height = brikHeight;
                        brick.Width = brikWidth;
                        brick.XCoordinate = Convert.ToInt32(node["xCoordinate"].InnerText);
                        brick.YCoordinate = Convert.ToInt32(node["yCoordinate"].InnerText);
                        bricks.Add(brick);
                    }
                    catch
                    {

                    }
                }

                XmlNodeList nodeList2 = doc.SelectNodes("/Structures/BricksB/BrickB");
                List<Bricks> bricksB = new List<Bricks>();
                foreach (XmlNode node in nodeList2)
                {
                    try
                    {
                        Bricks brick = new Bricks();
                        brick.Height = brikHeight;
                        brick.Width = brikWidth;
                        brick.XCoordinate = Convert.ToInt32(node["xCoordinate"].InnerText);
                        brick.YCoordinate = Convert.ToInt32(node["yCoordinate"].InnerText);
                        bricksB.Add(brick);
                    }
                    catch
                    {

                    }
                }

                XmlNodeList nodeList3 = doc.SelectNodes("/Structures/BricksQ/BrickQ");
                List<Bricks> bricksQ = new List<Bricks>();
                foreach (XmlNode node in nodeList3)
                {
                    try
                    {
                        Bricks brick = new Bricks();
                        brick.Height = brikHeight;
                        brick.Width = brikWidth;
                        brick.XCoordinate = Convert.ToInt32(node["xCoordinate"].InnerText);
                        brick.YCoordinate = Convert.ToInt32(node["yCoordinate"].InnerText);
                        brick.value = 1;
                        bricksQ.Add(brick);
                    }
                    catch
                    {

                    }
                }

                XmlNodeList nodeList4 = doc.SelectNodes("/Structures/Waters/Water");
                List<Bricks> water = new List<Bricks>();
                foreach (XmlNode node in nodeList4)
                {
                    try
                    {
                        Bricks brick = new Bricks();
                        brick.Height = brikHeight * 2;
                        brick.Width = Convert.ToInt32(node["width"].InnerText);
                        brick.XCoordinate = Convert.ToInt32(node["xCoordinate"].InnerText);
                        brick.YCoordinate = groundY;
                        water.Add(brick);
                    }
                    catch
                    {

                    }
                }

                XmlNodeList nodeList5 = doc.SelectNodes("/Structures/Pipes/Pipe");
                List<Bricks> pipes = new List<Bricks>();
                foreach (XmlNode node in nodeList5)
                {
                    try
                    {
                        Bricks brick = new Bricks();
                        brick.Height = Convert.ToInt32(node["heigth"].InnerText);
                        brick.Width = 2 * brikWidth;
                        brick.XCoordinate = Convert.ToInt32(node["xCoordinate"].InnerText);
                        brick.YCoordinate = Convert.ToInt32(node["yCoordinate"].InnerText);
                        pipes.Add(brick);
                    }
                    catch
                    {

                    }
                }


                XmlNodeList nodeList6 = doc.SelectNodes("/Structures/Castle");
                Bricks castle = new Bricks();
                foreach (XmlNode node in nodeList6)
                {
                    try
                    {
                        castle.Height = Convert.ToInt32(node["heigth"].InnerText);
                        castle.Width = Convert.ToInt32(node["width"].InnerText);
                        castle.XCoordinate = Convert.ToInt32(node["xCoordinate"].InnerText);
                        castle.YCoordinate = Convert.ToInt32(node["yCoordinate"].InnerText);
                    }
                    catch
                    {

                    }
                }

                gameObject.Bricks = bricks.ToArray();
                gameObject.BricksB = bricksB.ToArray();
                gameObject.BricksQ = bricksQ.ToArray();
                gameObject.Water = water.ToArray();
                gameObject.Pipes = pipes.ToArray();
                gameObject.Castle = castle;
            
            return gameObject;
        }

    }
}
